package com.turbo.cloud.ums.web.webservice;

import com.turbo.cloud.core.api.R;
import com.turbo.cloud.core.api.Result;
import com.turbo.cloud.core.validated.*;
import com.turbo.cloud.ums.web.logic.ApiLogic;
import com.turbo.cloud.ums.web.model.api.UmsApiQo;
import com.turbo.cloud.ums.web.model.api.UmsApiDo;
import com.turbo.cloud.ums.web.model.api.UmsApiVo;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 接口表 前端控制器
 * </p>
 *
 * @author zhangluning
 * @since 2022-10-11
 */
@RequiredArgsConstructor
@RestController
public class UmsApiController {
    final ApiLogic apiLogic;

    /**
     * 根据菜单ID，查询接口列表
     *
     * @return
     */
    @PostMapping(name = "根据菜单ID，查询接口列表", value = "/webservice/api/queryApiListByMenuId")
    public Result<List<UmsApiVo>> queryApiListByMenuId(@RequestBody @Validated(SelectListValidated.class) UmsApiQo umsApiQo) {
        List<UmsApiVo> result = apiLogic.queryApiListByMenuId(umsApiQo);
        return R.result(result);
    }

    /**
     * 根据菜单ID为Null，查询接口列表
     *
     * @return
     */
    @PostMapping(name = "根据菜单ID为Null，查询接口列表", value = "/webservice/api/queryApiListByNoMenuId")
    public Result<List<UmsApiVo>> queryApiListByNoMenuId(@RequestBody @Validated(SelectListValidated.class) UmsApiQo umsApiQo) {
        List<UmsApiVo> result = apiLogic.queryApiListByNoMenuId(umsApiQo);
        return R.result(result);
    }

    /**
     * 根据菜单ID，查询接口列表，并根据权限ID标记选中
     *
     * @return
     */
    @PostMapping(name = "根据菜单ID，查询接口列表，并根据权限ID标记选中", value = "/webservice/api" + "/queryApiCheckedListByMenuIdOfPermissionId")
    public Result<List<UmsApiVo>> queryApiCheckedListByMenuIdOfPermissionId(@RequestBody @Validated(SelectMulWhereValidated.class) UmsApiQo umsApiQo) {
        List<UmsApiVo> result = apiLogic.queryApiCheckedListByMenuIdOfPermissionId(umsApiQo);
        return R.result(result);
    }

    /**
     * 新增接口
     *
     * @param umsApiDo 实体
     * @return
     */
    @PostMapping(name = "新增接口", value = "/webservice/api/createApi")
    public Result<Boolean> createApi(@RequestBody @Validated(InsertValidated.class) UmsApiDo umsApiDo) {
        boolean result = apiLogic.createApi(umsApiDo);
        return R.result(result);
    }

    /**
     * 修改接口
     *
     * @param umsApiDo 实体
     * @return
     */
    @PostMapping(name = "修改接口", value = "/webservice/api/modifyApi")
    public Result<Boolean> modifyApi(@RequestBody @Validated(UpdateValidated.class) UmsApiDo umsApiDo) {
        boolean result = apiLogic.modifyApi(umsApiDo);
        return R.result(result);
    }

    /**
     * 开启、关闭接口有效状态
     *
     * @param id 组件ID
     * @return Boolean
     */
    @GetMapping(name = "开启、关闭接口有效状态", value = "/webservice/api/enabledApiStateById")
    public Result<Boolean> enabledApiStateById(@RequestParam @NotBlank(message = "接口ID不能为空") String id) {
        boolean result = apiLogic.enabledApiStateById(id);
        return R.result(result);
    }

    /**
     * 删除接口
     *
     * @param id 接口ID
     * @return
     */
    @GetMapping(name = "删除接口", value = "/webservice/api/removeApiById")
    public Result<Boolean> removeApiById(@NotBlank(message = "接口ID不能为空") String id) {
        boolean result = apiLogic.removeApiById(id);
        return R.result(result);
    }

    /**
     * 绑定接口关联的菜单
     *
     * @param umsApiDo 实体
     */
    @PostMapping(name = "新增接口", value = "/webservice/api/bindApiRelationMenu")
    public Result<Boolean> bindApiRelationMenu(@RequestBody UmsApiDo umsApiDo) {
        boolean result = apiLogic.bindApiRelationMenu(umsApiDo);
        return R.result(result);
    }
}
